package com.android.launcher3.util;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSyntaxException;

import java.lang.reflect.Modifier;
import java.lang.reflect.Type;


public class JsonTools {

    public static <T> T parseJson2Bean(String json, Type type) {
        return parseJson2Bean(json, type, null);
    }

    public static <T> T parseJson2Bean(String json, Type type, int modifier) {
        return parseJson2Bean(json, type, null, modifier);
    }

    /**
     * parse Json to class
     *
     * @param clazz bean
     */
    public static <T> T parseJson2Bean(String json, Class<T> clazz) {
        return parseJson2Bean(json, clazz, Modifier.TRANSIENT, Modifier.STATIC);
    }

    public static <T> T parseJson2BeanFromAdapter(String json, Type type, TypeAdapter<?> typeAdapter) {

        return parseJson2Bean(json, type, typeAdapter, Modifier.TRANSIENT, Modifier.STATIC);
    }

    public static String parseObject2JsonString(Object bean) {

        return parseObject2JsonString(bean, Modifier.TRANSIENT, Modifier.STATIC);
    }

    /**
     * parse Json to class
     *
     * @param clazz     bean
     * @param modifiers the field modifiers. You must use the modifiers specified in the
     */
    public static <T> T parseJson2Bean(String json, Class<T> clazz, int... modifiers) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }
//        mGson = new Gson();
        GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping();
        if (modifiers != null) {
            gsonBuilder.excludeFieldsWithModifiers(modifiers);
        }
        Gson mGson = gsonBuilder.create();//不进行unicode转译
        T t = null;
        try {
            t = mGson.fromJson(json, clazz);
        } catch (JsonSyntaxException e) {
            Logger.e("parseJson2Bean", "parseJson2Bean...requestFailed!!!");
            e.printStackTrace();
        }
        return t;
    }


    /**
     * @param modifiers the field modifiers. You must use the modifiers specified in the
     */
    public static <T> T parseJson2Bean(String json, Type type, TypeAdapter<?> typeAdapter, int... modifiers) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }
//        mGson = new Gson();
        GsonBuilder gsonBuilder = new GsonBuilder();
        if (modifiers != null) {
            gsonBuilder.excludeFieldsWithModifiers(modifiers);
        }
        if (typeAdapter != null) {
            gsonBuilder.registerTypeAdapter(typeAdapter.getType(), typeAdapter);
        }
        Gson mGson = gsonBuilder.disableHtmlEscaping().create();//不进行unicode转译
        T t = null;
        try {
            t = mGson.fromJson(json, type);
        } catch (JsonSyntaxException e) {
            Logger.e("parseJson2Bean", "parseJson2Bean...requestFailed!!!");
            e.printStackTrace();
        }
        return t;
    }


    public static <T> T parseJson2BeanTypeArr(String json, Type type, TypeAdapter<?>... typeAdapter) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        GsonBuilder gsonBuilder = new GsonBuilder();
        if (typeAdapter != null && typeAdapter.length != 0) {
            for (TypeAdapter<?> adapter : typeAdapter) {
                if (adapter != null) {
                    gsonBuilder.registerTypeAdapter(adapter.getType(), typeAdapter);
                }
            }
        }

        Gson mGson = gsonBuilder.disableHtmlEscaping().create();//不进行unicode转译
        T t = null;
        try {
            t = mGson.fromJson(json, type);
        } catch (JsonSyntaxException e) {
            Logger.e("parseJson2Bean", "parseJson2Bean...requestFailed!!!");
            e.printStackTrace();
        }
        return t;
    }

    public static <T> T parseJson2BeanTypeArr(String json, Class<T> clazz, TypeAdapter<?>... typeAdapter) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        GsonBuilder gsonBuilder = new GsonBuilder();
        if (typeAdapter != null && typeAdapter.length != 0) {
            for (TypeAdapter<?> adapter : typeAdapter) {
                if (adapter != null) {
                    gsonBuilder.registerTypeAdapter(adapter.getType(), typeAdapter);
                }
            }
        }

        Gson mGson = gsonBuilder.disableHtmlEscaping().create();//不进行unicode转译
        T t = null;
        try {
            t = mGson.fromJson(json, clazz);
        } catch (JsonSyntaxException e) {
            Logger.e("parseJson2Bean", "parseJson2Bean...requestFailed!!!");
            e.printStackTrace();
        }
        return t;
    }


    /**
     * @param modifiers the field modifiers. You must use the modifiers specified in the
     */
    public static String parseObject2JsonString(Object bean, int... modifiers) {
        if (bean == null) {
            return null;
        }
        GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping();
        if (modifiers != null) {
            gsonBuilder.excludeFieldsWithModifiers(modifiers);
        }
        Gson mGson = gsonBuilder.create();//不进行unicode转译
        String mJson = "";
        try {
            mJson = mGson.toJson(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mJson;
    }

    public abstract static class TypeAdapter<T> implements JsonDeserializer<T> {
        public TypeAdapter(@NonNull Type type) {
            this.type = type;
        }

        private Type type;

        public Type getType() {
            return type;
        }

        public void setType(Type type) {
            this.type = type;
        }
    }
}
